# frozen_string_literal: true

module QA
  module EE
    module Page
      module Project
        module Secure
          class VulnerabilityDetails < QA::Page::Base
            view 'ee/app/assets/javascripts/vulnerabilities/components/header.vue' do
              element 'vulnerability-header'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/vulnerability_details.vue' do
              element 'vulnerability-details'
              element 'title'
              element 'description'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/footer.vue' do
              element 'vulnerability-footer'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/related_issues.vue' do
              element 'create-issue-button'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/false_positive_alert.vue' do
              element 'false-positive-alert'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/vulnerability_state_dropdown.vue' do
              element 'vulnerability-status-dropdown'
              element 'vulnerability-save-status-button'
              element 'dismissal-reason-dropdown'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/related_jira_issues.vue' do
              element 'create-new-jira-issue-link'
              element 'jira-issue-link'
            end

            view 'ee/app/assets/javascripts/vulnerabilities/components/vulnerability_training.vue' do
              element 'security-training-text'
              element 'security-training-link'
            end

            def has_component?(component_name:)
              has_element?(component_name)
            end

            def has_vulnerability_title?(title:)
              has_element?('title', text: title)
            end

            def security_training_present?(training_name:)
              has_element?('security-training-text', text: training_name)
            end

            def training_link_present?(training_name:, url:)
              element = find_element('security-training-link', training_name: training_name, wait: 20)
              element["href"].include?(url)
            end

            def has_vulnerability_description?(description:)
              has_element?('description', text: description)
            end

            def click_create_issue_button
              click_element('create-issue-button')
            end

            def click_create_jira_issue_button
              click_element('create-new-jira-issue-link')
            end

            def change_status(status)
              click_element('vulnerability-status-dropdown')
              click_element(status)

              choose_dismissal_reason if status == 'dismissed'

              click_element('vulnerability-save-status-button')
            end

            def choose_dismissal_reason
              reasons = ['Acceptable risk', 'False positive', 'Mitigating control', 'Used in tests', 'Not applicable']
              return unless has_element?('dismissal-reason-dropdown')

              find_element('dismissal-reason-dropdown').find('li', text: reasons.sample).click
            end

            def training_header_present?
              has_css?('h3', text: 'Training')
            end

            def has_vulnerability_status?(status)
              has_element?('vulnerability-status-dropdown', text: "#{status.capitalize}")
            end

            def jira_issue_link_present?(issue_text, jira_issue_url, issue_key)
              element = find_element('jira-issue-link', issue_key: issue_key)
              element.text == issue_text && element["href"].include?(jira_issue_url)
            end
          end
        end
      end
    end
  end
end
